import {loadEnvApi} from "./load_env.js";
import {createServer } from "vite";
import vue from '@vitejs/plugin-vue';
import vueSetupExtend from 'vite-plugin-vue-setup-extend';
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';

import { resolve } from "path";
import {parseConfName} from "./load_env.js";
import HtmlInject from "./../plugins/vite-plugin-html-injecet.js";


async function exec(){
   await loadEnvApi("development");
   console.log("loadEnvApi  success");
  
   const server = await createServer({
    configFile: false,
    root:process.cwd(),
    build:{
      // target:["es2015"],
      minify:true,  //打包结果取消minify，方便我们看打包后结果对比
    rollupOptions: {
      output: {
        chunkFileNames: 'js/[name]-[hash].js',  // 引入文件名的名称
        entryFileNames: 'js/[name]-[hash].js',  // 包的入口文件名称
        assetFileNames: '[ext]/[name]-[hash].[ext]', // 资源文件像 字体，图片等
      }
    },
    chunkSizeWarningLimit:1500
    },
    server:{
      host:true,
      hmr:true,
     
        port:3000,
 
      },
      plugins: [
    
        vue(),
        vueSetupExtend(),
        AutoImport({
          resolvers: [ElementPlusResolver({
            exclude: /^ElAmap[A-Z]*/
          })],
        }),
        Components({
          resolvers: [ElementPlusResolver({
            exclude: /^ElAmap[A-Z]*/
          })],
        }),
        HtmlInject("developement"),
      ],
      resolve:{
        alias:{
          "~": resolve("./src"),
          "~components":resolve("./src/components"),
          "~api": resolve("./src/api/api.ignore.js"),
          "~tools":resolve("./src/public/tools"),
          "~assets":resolve("./src/assets"),
          "~public":resolve("./src/public"),
          "~Router":resolve("./src/public/tools/Router.js"),
          "~Rules":resolve("./src/public/rules/index.js"),
        },
      },
      define:{
        __ENV__:JSON.stringify(parseConfName()),
        __MODEL__:JSON.stringify("development"),
      }
   });
   await server.listen()

   server.printUrls()
};

exec();